package com.mdp.workflow.biz.hi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.service.BaseService;
import com.mdp.workflow.biz.hi.entity.Comment;
import com.mdp.workflow.biz.hi.mapper.CommentMapper;
import com.mdp.workflow.client.service.WfSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.mdp.core.utils.CollectionUtils.convertMap;

/**
 * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> 
 * 组织 com.mdp  顶级模块 workflow 大模块 hi 小模块 <br>
 * 实体 Comment 表 act_hi_comment 当前主键(包括多主键): id_; 
 ***/
@Service
public class CommentService extends BaseService<CommentMapper, Comment> {
	static Logger logger =LoggerFactory.getLogger(CommentService.class);
	@Autowired
	WfSysUserService wfSysUserService;
	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		List<Map<String,Object>> datas = baseMapper.selectListMapByWhere(page,ew,ext);
 		if(datas==null||datas.size()==0) {
			return datas;
		}
		Set<String> set=new HashSet<>();
		for (Map<String, Object> data : datas) {
			set.add((String) data.get("userid"));
		}
		Map<String,String> umap=!set.isEmpty()? convertMap(wfSysUserService.selectSimpleUsersByUserids(set),o->o.get("userid"),o->o.get("username")):null;
		if(umap==null || umap.size()==0){
			return datas;
		}
		for (Map<String, Object> data : datas) {
			String id= (String) data.get("userid");
			data.put("username",umap.get(id));
		}
		return datas;
	}
}

